import Vue from 'vue'
import moment from 'dayjs'
Vue.filter('fixTwoNumber', (value = 0) => {
  let val = Math.round(value * 100) / 100
  return val.toFixed(2)
})
Vue.filter('dateformat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  // 判断数据如果为空返回'--' 否则返回日期
  if (!dataStr) {
    return ' -- '
  }
  return moment(dataStr).format(pattern)
})

Vue.filter('formatDate',function(date){
  const dateTime = new Date(date);
  const YY = dateTime.getFullYear();
  const MM =
    dateTime.getMonth() + 1 < 10
      ? '0' + (dateTime.getMonth() + 1)
      : dateTime.getMonth() + 1;
  const D =
    dateTime.getDate() < 10 ? '0' + dateTime.getDate() : dateTime.getDate();
  const hh =
    dateTime.getHours() < 10
      ? '0' + dateTime.getHours()
      : dateTime.getHours();
  const mm =
    dateTime.getMinutes() < 10
      ? '0' + dateTime.getMinutes()
      : dateTime.getMinutes();
  const ss =
    dateTime.getSeconds() < 10
      ? '0' + dateTime.getSeconds()
      : dateTime.getSeconds();
  return `${YY}-${MM}-${D}`;
})

Vue.filter('getAllDate',function(begin, end){
      let arr1= begin.split("-");
      let arr2= end.split("-");
      let arr1_= new Date();
      let arrTime = [];
      arr1_.setUTCFullYear(arr1[0], arr1[1] - 1, arr1[2]);
      let arr2_= new Date();
      arr2_.setUTCFullYear(arr2[0], arr2[1] - 1, arr2[2]);
      let unixDb = arr1_.getTime();
      let unixDe = arr2_.getTime();
      for (let k = unixDb; k <= unixDe;) {
        arrTime.push(this.datetimeparse(k, 'YYY-MM-DD'));
        k = k + 24 * 60 * 60 * 1000;
      }
      return arrTime;
})

Vue.filter('datetimeparse',function(timestamp, format, prefix){
     
    // 时间格式处理
      if (typeof timestamp =='string'){
          timestamp=Number(timestamp)
      };
      //转换时区
      let currentZoneTime = new Date (timestamp);
      let currentTimestamp = currentZoneTime.getTime ();
      let offsetZone = currentZoneTime.getTimezoneOffset () / 60;//如果offsetZone>0是西区，西区晚
      let offset = null;
      //客户端时间与服务器时间保持一致，固定北京时间东八区。
      offset = offsetZone + 8;
      currentTimestamp = currentTimestamp + offset * 3600 * 1000

      let newtimestamp = null;
      if (currentTimestamp) {
          if (currentTimestamp.toString ().length === 13) {
              newtimestamp = currentTimestamp.toString ()
          } else if (currentTimestamp.toString ().length === 10) {
              newtimestamp = currentTimestamp + '000'
          } else {
              newtimestamp = null
          }
      } else {
          newtimestamp = null
      }
      ;
      let dateobj = newtimestamp ? new Date (parseInt (newtimestamp)) : new Date ()
      let YYYY = dateobj.getFullYear ()
      let MM = dateobj.getMonth () > 8 ? dateobj.getMonth () + 1 : '0' + (dateobj.getMonth () + 1)
      let DD = dateobj.getDate () > 9 ? dateobj.getDate () : '0' + dateobj.getDate ()
      let HH = dateobj.getHours () > 9 ? dateobj.getHours () : '0' + dateobj.getHours ()
      let mm = dateobj.getMinutes () > 9 ? dateobj.getMinutes () : '0' + dateobj.getMinutes ()
      let ss = dateobj.getSeconds () > 9 ? dateobj.getSeconds () : '0' + dateobj.getSeconds ()
      let output = '';
      let separator = '-'
      if (format) {
          separator = format.match (/-/) ? '-' : '-'
          output += format.match (/yy/i) ? YYYY : ''
          output += format.match (/MM/) ? (output.length ? separator : '') + MM : ''
          output += format.match (/dd/i) ? (output.length ? separator : '') + DD : ''
          output += format.match (/hh/i) ? (output.length ? ' ' : '') + HH : ''
          output += format.match (/mm/) ? (output.length ? ':' : '') + mm : ''
          output += format.match (/ss/i) ? (output.length ? ':' : '') + ss : ''
      } else {
          output += YYYY + separator + MM + separator + DD
      }
      output = prefix ? (prefix + output) : output

      return newtimestamp ? output : ''

})

Vue.filter('isExistData',function(num, arr1){
    //定义一个函数
      for (var j = 0; j < arr1.length; j++) {
        if (num === arr1[j]) {
          return true; //如果传过来的元素在arr1中能找到相匹配的元素，我们返回fasle
        }
      }
      return false; //如果不能找到相匹配的元素，返回true
})

Vue.filter('getArrDifference',function(arr1, arr2){
  return arr1.concat(arr2).filter(function (v, i, arr) {
      return arr.indexOf(v) === arr.lastIndexOf(v);
  });
})

Vue.filter('toThousands', function (value) {
  let result = ''
  let num = (value || 0).toString()
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result
    num = num.slice(0, num.length - 3)
  }
  if (num) {
    result = num + result
  }
  return result
})
Vue.filter('convertImgToBase64', function (imageFile) {

  let reader = new FileReader();
  // eslint-disable-next-line
  let imgFile = null;
  reader.readAsDataURL(imageFile.row)
  reader.onload = e => {
    imgFile = e.target.result;
  }
  // eslint-disable-next-line
  console.log('imgFile=',imgFile)
  return reader
})